/*
 * $Id: Login.java 471756 2006-11-06 15:01:43Z husted $
 *
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

package com.nk.system.action;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;

import com.nk.system.beans.UserProfileBean;
import com.nk.system.beans.UsersBean;
import com.nk.system.service.TransactionsService;
import com.nk.system.utils.UI_Constants;

public class LoginAction extends ActionBaseSupport {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private Logger log = Logger.getLogger(LoginAction.class);
	
	@Autowired
	private TransactionsService transactionsService;

	/**
	 * Parameter Form
	 */
    private String username;
    private String password;
    
	public String init() throws Exception {
		log.info("#init");
		clearErrorsAndMessages();
		return INPUT;
	}

    public String execute() throws Exception {
    	log.info("#execute");
    	clearErrorsAndMessages();
    	
    	validate();
    	if(getActionMessages().size() > 0){
        	return INPUT;
    	}
    	
    	/**
    	 * Fix Admin
    	 */
    	if(getUsername().equalsIgnoreCase("admin") && getPassword().equals("P@ssw0rd")) {

            UserProfileBean userProfileBean = new UserProfileBean();
            userProfileBean.setUsername(getUsername());
            userProfileBean.setPassword(getPassword());
            
            getServletRequest().getSession().setAttribute(UI_Constants.SESS_ATTR_USER_ACCESS,userProfileBean);

            return SUCCESS;
    	}
    	
    	/**
    	 * Login
    	 */
    	UsersBean usersBean = transactionsService.authentication(getUsername(), getPassword());
    	if(usersBean != null) {
    		UserProfileBean UserProfileBean = new UserProfileBean();
    		BeanUtils.copyProperties(UserProfileBean, usersBean);
            getServletRequest().getSession().setAttribute(UI_Constants.SESS_ATTR_USER_ACCESS,UserProfileBean);

            return SUCCESS;
    	}else {
    		addActionMessage(getText("login.fail"));
    		
    		return INPUT;
    	}
        
    }
    
    @Override
    public void validate() {
    	// TODO Auto-generated method stub
    	super.validate();
    	if (isInvalid(getUsername())) {
    		addActionMessage(getText("requiredstring",new String[]{"Username"}));
    		return;
    	}

        if (isInvalid(getPassword())) {
        	addActionMessage(getText("requiredstring",new String[]{"Password"}));
        	return;
        }
    }

    private boolean isInvalid(String value) {
        return (value == null || value.length() == 0);
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

}